package ch.cm.android.teamlearn;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import ch.cm.android.teamlearn.gcm.GCMRegistrator;
import ch.cm.android.teamlearn.webservice.WebRestSender;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {

    private static final String TAG = "GCMIntentService";

    public GCMIntentService() {
        super(GCMRegistrator.PROJECT_ID);
        Log.d(TAG, "GCMIntentService init");
    }

    @Override
    protected void onError(Context ctx, String sError) {
        // TODO Auto-generated method stub
        Log.d(TAG, "Error: " + sError);

    }

    @Override
    protected void onMessage(Context ctx, Intent intent) {

        Log.d(TAG, "Message Received");

        String message = intent.getStringExtra("message");

        sendGCMIntent(ctx, message);

    }

    private void sendGCMIntent(Context ctx, String message) {

        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("GCM_RECEIVED_ACTION");

        broadcastIntent.putExtra("gcm", message);

        ctx.sendBroadcast(broadcastIntent);

    }

    @Override
    protected void onRegistered(Context ctx, String regId) {
        sendRegistrationID2Server(ctx, regId);
        Log.d(TAG, regId);

    }

    @Override
    protected void onUnregistered(Context ctx, String regId) {
        // TODO Auto-generated method stub
        // send notification to your server to remove that regId

    }

    private void sendRegistrationID2Server(Context ctx, String regID) {
        WebRestSender restSender = new WebRestSender(ctx);
        Log.d(TAG, "SenderStatus: "+restSender.sendRequest(regID));
    }
}